package controllerLayer;


import java.io.IOException;
import java.util.ArrayList;

import modelLayer.*;

public class ProductCtr { 

	private SLIRecords srec;
	private ProductRecords prec;
	private ContractorRecords corec;
	
	public ProductCtr(){
		prec = ProductRecords.getInstance();
		srec = SLIRecords.getInstance();
		corec = ContractorRecords.getInstance();

		
		
	}
   

	
	
	public int createItem(String contrID,String measure, String name, String description, double price, double quantity, String category, String location, double alert)
	{
		return prec.createItem(contrID, measure, name, description, price, quantity, category, location, alert);
		}
		
		
	public Contractor findContractor(String id){
		return corec.findContractor(id);
	}
	public String getContractor(String id){
		return corec.getContractor(id);
	}
	
	
	public String findItem(int Id)
	{
		return prec.findItem(Id);
	}
	
	
	public Product getItem(String prodID) {
		return prec.getItem(prodID);

	}
	
	public SaleLinesItem getSaleLinesItem(String ID){
		return srec.getSaleLinesItem(ID);
	}
	
	public void countQuantity(String ID, double quantity) throws IllegalArgumentException{
		if(getSaleLinesItem(ID).getItemCollection()!=null){
			for(Product product: getSaleLinesItem(ID).getItemCollection()){
				if(product.getQuantity()<quantity){
					throw new IllegalArgumentException("Not enough products!");
				}
			}
				for(Product product: getSaleLinesItem(ID).getItemCollection()){
					product.setQuantity(product.getQuantity()-quantity);
					getSaleLinesItem(ID).setBuyQuantity(quantity);
					}
			}
		else{
		if(getSaleLinesItem(ID).getProduct().getQuantity() >= quantity){
			getSaleLinesItem(ID).getProduct().setQuantity(getSaleLinesItem(ID).getProduct().getQuantity() - quantity);
		getSaleLinesItem(ID).setBuyQuantity(quantity);
		}
		else throw new IllegalArgumentException("Not enough products!");
		if (isRunningOut(getSaleLinesItem(ID).getProduct()))System.out.println("Product is running out!");
		}
	}
	/*
	 * update item even if company makes a new order of product, which exist in system
	 * 
	 */
	public void updateItem(String Id, String contID, double quantity, String location, double price, double alert)
	{
		prec.updateItem(Id, contID, quantity, location, price, alert);
	}
	public void deleteItem(String id)
	{
		prec.deleteItem(id);
	}
	
	public void sellProduct(String empID,String custID, ArrayList<SaleLinesItem>sls)
	{
		double discount = 1 - CustomerRecords.getInstance().getCustomer(custID).getDiscount()/100;
		
		
			for(SaleLinesItem sl2: sls){
					double quantity = sl2.getBuyQuantity();
					String name =sl2.getName();
					String id = sl2.getID();
							
					CustomerRecords.getInstance().buyItem(custID, name, quantity, id, discount);
					EmployeeRecords.getInstance().sellItem(empID, name, quantity, id, discount);	
				
				}
		
		
		}
	
	public boolean isRunningOut(Product product){
		if(product.getAlert()>=product.getQuantity()) return true;
		else return false;
	}
	public void deleteContractor(String id) {
		corec.deleteContractor(id);
		}
	
	public void updateContractor(String id, String address, String phoneNr) {
		corec.updateContractor(id, address, phoneNr);
	}
	
	public void createContractor(String id, String name, String address, String phoneNr) {
		corec.createContractor(id, name, address, phoneNr);
		
	}
	public void saveContr() throws IOException{
		corec.saveContr();
	}
	public void saveProd() throws IOException{
		prec.saveProd();
	}
	public void saveSLI() throws IOException{
		srec.saveSLI();
			
		}
		public void createSLI(Product product){
			srec.createSLI(product);
		}
		public String createCollection(String name){
			return  srec.createCollection(name);
			 
		}

		public void addToSLI(int id, String collectionID){
			srec.addToSLI(id, collectionID);
	}




		
	
}
